package src.main.java.com.pms.gui.dialog.sale;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import src.main.java.com.pms.service.SaleService;

public class UpdateSalePriceDialog extends JDialog {
    private JTextField petIdField;
    private JTextField priceField;
    private JTextField contactField;
    private SaleService saleService;





    private void initComponents() {
        setSize(300, 150);
        setLocationRelativeTo(getParent());

        JPanel panel = new JPanel(new GridLayout(4, 2, 10, 10));

        JLabel petIdLabel = new JLabel("宠物ID:");
        petIdField = new JTextField();
        petIdField.setEditable(false); // 禁止编辑宠物ID

        JLabel priceLabel = new JLabel("新价格:");
        priceField = new JTextField();

        JLabel contactLabel = new JLabel("卖家联系方式:");
        contactField = new JTextField();

        JButton updateButton = new JButton("更新");
        updateButton.addActionListener(new UpdateActionListener());

        panel.add(petIdLabel);
        panel.add(petIdField);
        panel.add(priceLabel);
        panel.add(priceField);
        panel.add(contactLabel);
        panel.add(contactField);
        panel.add(new JLabel());
        panel.add(updateButton);

        add(panel);
    }

    public UpdateSalePriceDialog(JFrame parent) {
        super(parent, "更新寄卖价格", true);
        saleService = new SaleService();
        initComponents();
        requestFocusInWindow(); // 确保对话框获得焦点
        setVisible(true); // 显示对话框
        priceField.requestFocusInWindow(); // 确保价格输入框获得焦点
    }


    public UpdateSalePriceDialog(JFrame parent, int petId, double price, String contact) {
        super(parent, "更新寄卖价格", true);
        saleService = new SaleService();
        initComponents();
        petIdField.setText(String.valueOf(petId));
        priceField.setText(String.valueOf(price));
        contactField.setText(contact);
        requestFocusInWindow(); // 确保对话框获得焦点
        setVisible(true); // 显示对话框
        priceField.requestFocusInWindow(); // 确保价格输入框获得焦点
    }

    public UpdateSalePriceDialog(SaleListDialog saleListDialog, int id, double askingPrice, String sellerContact) {
        super(saleListDialog, "更新寄卖价格", true);
        saleService = new SaleService();
        initComponents();
        petIdField.setText(String.valueOf(id));
        priceField.setText(String.valueOf(askingPrice));
        contactField.setText(sellerContact);
        requestFocusInWindow(); // 确保对话框获得焦点
        setVisible(true); // 显示对话框
        priceField.requestFocusInWindow(); // 确保价格输入框获得焦点
    }

    private class UpdateActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                int petId = Integer.parseInt(petIdField.getText().trim());
                double price = Double.parseDouble(priceField.getText().trim());
                String contact = contactField.getText().trim();

                if (petId <= 0 || price <= 0 || contact.isEmpty()) {
                    JOptionPane.showMessageDialog(UpdateSalePriceDialog.this, "请输入有效的信息", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                saleService.updateSalePrice(petId, price);
                saleService.updateSellerContact(petId, contact); // 假设有一个方法来更新卖家联系方式
                JOptionPane.showMessageDialog(UpdateSalePriceDialog.this, "寄卖信息更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(UpdateSalePriceDialog.this, "请输入有效的数字", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
